<?php

/**
 *
 * @copyright 	Copyright (c) 2011, Globtec (http://www.globtec.com.br)
 * @author 		Jose Carlos Soares de Souza <josecarlos@globtec.com.br>
 */
class App_Plugins_PluginLoader extends Zend_Controller_Plugin_Abstract
{
	/**
	 * 
	 * @var array
	 */
	private $_plugins = array();

	/**
	 * 
	 * @param string $module
	 * @param string $pluginName
	 */
	public function addPlugin ( $module, $pluginName )
	{
		$module = strtolower($module);
		
		if ( ! isset($this->_plugins[$module]) ) 
			$this->_plugins[$module] = array();
		
		$this->_plugins[$module][] = $pluginName;
	}

	/**
	 * 
	 * (non-PHPdoc)
	 * @see Zend_Controller_Plugin_Abstract::routeShutdown()
	 */
	public function routeShutdown ( Zend_Controller_Request_Abstract $request )
	{
		$module = strtolower($request->getModuleName());
		
		$front = Zend_Controller_Front::getInstance();
		
		if ( empty($this->_plugins[$module]) ) 
			return;
		
		foreach ( $this->_plugins[$module] as $pluginName )
			$front->registerPlugin(new $pluginName());
	}
}